package com.woniu.carrent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.carrent.mapper.TCarMapper;
import com.woniu.carrent.pojo.TCar;
import com.woniu.carrent.pojo.dto.TCarDto;
import com.woniu.carrent.pojo.vo.TCarVo;
import com.woniu.carrent.service.TCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TCarServiceImpl extends ServiceImpl<TCarMapper, TCar> implements TCarService {
    @Autowired
    private TCarMapper tCarMapper;
    @Override
    public List<TCar> selectCars(Page<TCar> page, TCarDto conditions) {
        QueryWrapper<TCar> queryWrapper = new QueryWrapper<>();

        // 添加查询条件 1.类型查询 2.品牌查询 3.车牌查询 4价格区间查询 5.是否在线查询 6.是否删除查询 7.根据所属城市查询
        if (conditions.getCtypeid() != null && conditions.getCtypeid() != 0) {
            queryWrapper.apply("c.cTypeId = {0}", conditions.getCtypeid());  // 类型
        }
        if (conditions.getBrandid() != null && conditions.getBrandid() != 0) {
            queryWrapper.apply("c.brandId = {0}", conditions.getBrandid());// 品牌
        }
        if (conditions.getPlate() != null && !conditions.getPlate().isEmpty()) {
            queryWrapper.apply("c.plate = {0}", conditions.getPlate());//车牌
        }
        if (conditions.getMinPrice() != null) {
            queryWrapper.apply("c.price >= {0}", conditions.getMinPrice());
        }
        if (conditions.getMaxPrice() != null) {
            queryWrapper.apply("c.price <= {0}", conditions.getMaxPrice());
        }
        if (conditions.getLid() != null && conditions.getLid() != 0) {
            queryWrapper.apply("c.Lid = {0}", conditions.getLid());//所属城市
        }
        if (conditions.getIsonline() != null ) {
            queryWrapper.apply("c.isOnline = {0}", conditions.getIsonline());//是否在线
        }
        if (conditions.getIsdeleted() != null) {
            queryWrapper.apply("c.isDeleted = {0}", conditions.getIsdeleted());//是否被删除
        }

        List<TCar> list = tCarMapper.queryCar(page, queryWrapper);
        return list;
    }
    @Override
    public List<TCar> getCarsByBrandName(String brandName) {
        return tCarMapper.selectCarsByBrandName(brandName);
    }
}
